package com.pansoft.openplanet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.utils.LXGlideImageLoader;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.BoxParentBean;
import com.pansoft.openplanet.bean.BoxResourceBean;

import java.util.List;

public class ResourceAdapter extends BaseExpandableListAdapter {
    private List<BoxParentBean> parentBeanList;
    private Context mContext;
    private LayoutInflater inflater;

    public ResourceAdapter(Context mContext, List<BoxParentBean> parentBeanList) {
        this.parentBeanList = parentBeanList;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public int getGroupCount() {
        return parentBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return parentBeanList.get(groupPosition).getResourceBeanList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentBeanList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return parentBeanList.get(groupPosition).getResourceBeanList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        BoxParentBean parentBean = parentBeanList.get(groupPosition);
        GroupViewHolder holder = null;
        if (convertView == null) {
            holder = new GroupViewHolder();
             convertView = inflater.inflate(R.layout.talk_chain_item_resource_group, null);
            holder.ivParentIcon = convertView.findViewById(R.id.iv_parent_icon);
            holder.tvParentTitle = convertView.findViewById(R.id.tv_parent_title);
            holder.tvParentCount = convertView.findViewById(R.id.tv_parent_count);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }

        holder.tvParentTitle.setText(parentBean.getName());
        holder.tvParentCount.setText("全部(" + parentBean.getResourceBeanList().size() + ")");
        LXGlideImageLoader.getInstance().displayImage(mContext, holder.ivParentIcon, parentBean.getIcon()
                , R.drawable.talk_chain_point_icon, R.drawable.talk_chain_point_icon);
        return convertView;


    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        BoxResourceBean resourceBean = parentBeanList.get(groupPosition).getResourceBeanList().get(childPosition);
        ChildViewHolder holder = null;
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.talk_chain_item_resource_child, null);
            holder.ivChildIcon = convertView.findViewById(R.id.iv_child_icon);
            holder.tvChildTitle = convertView.findViewById(R.id.tv_child_title);
            holder.tvSize = convertView.findViewById(R.id.tv_size);
            holder.tvTime = convertView.findViewById(R.id.tv_time);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        holder.tvChildTitle.setText(resourceBean.getName());
        holder.tvTime.setText(resourceBean.getTime());
        holder.tvSize.setText(resourceBean.getSize());
        LXGlideImageLoader.getInstance().displayImage(mContext, holder.ivChildIcon, resourceBean.getPreImage()
                , R.drawable.talk_chain_point_icon, R.drawable.talk_chain_point_icon);
        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    static class GroupViewHolder {
        ImageView ivParentIcon;
        TextView tvParentTitle;
        TextView tvParentCount;


    }

    static class ChildViewHolder {
        ImageView ivChildIcon;
        TextView tvChildTitle;
        TextView tvSize;
        TextView tvTime;


    }
}
